package cn.kgc.ssm.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.kgc.ssm.annotations.LogOperation;
import cn.kgc.ssm.dto.QueryStudent;
import cn.kgc.ssm.entity.Student;
import cn.kgc.ssm.enums.Operation;
import cn.kgc.ssm.service.StudentService;
import cn.kgc.ssm.util.ResponseResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author 课工场
 * @date 2023/10/27
 * @description 学生业务
 */
@RestController
@RequestMapping("student")
public class StudentController {

    @Resource
    private StudentService studentService;

    // 学生信息分页查询
    @RequestMapping("list")
    public ResponseResult studentList(QueryStudent queryStudent) {

        return studentService.studentPage(queryStudent);
    }


    @PostMapping("add")
    public ResponseResult addStudent(@RequestBody Student student) {

        System.out.println("student = " + student);

        return studentService.addStudent(student);
    }

    // 根据学号查询学生详细信息
    @GetMapping("getByStudentNo")
    public ResponseResult getByStudentNo(String studentNo) {

        return studentService.getByStudentNo(studentNo);
    }

    // 编辑学生信息
    @PostMapping("edit")
    @LogOperation(model = "学生",description = Operation.UPDATE)
    public ResponseResult editStudent(@RequestBody Student student) {

      return   studentService.editStudent(student);
    }


    // 根据id删除学生信息
    @GetMapping("delete")
    @LogOperation(model = "学生",description = Operation.DELETE)
    public ResponseResult deleteStudent(Integer id) {

        return   studentService.deleteStudent(id);
    }

    // 学生信息批量导入
    @PostMapping("batch")
    public  ResponseResult  batchAdd(@RequestParam("file") MultipartFile file){

        if (file.isEmpty()){
            throw  new RuntimeException("文件不能为空");
        }
        return  studentService.batchAdd(file);
    }


    @GetMapping("export")
    public void export(String clazz, HttpServletResponse response){

        //  1.根据班级的名称 查询学生数据
       List<Student> students =  studentService.getStudentsByClazz(clazz);

       // 2.将学生列表 以excel表格的形式进行导出
        ExportParams exportParams = new ExportParams("学生信息", clazz);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Student.class, students);
        try {

            // 设置文件的响应格式  以附件的形式进行打开  下载
            response.setContentType("application/force-download");
            // 设置文件下载后的文件名
            response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("学生信息表.xls","UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

}
